﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Text.RegularExpressions;

namespace aucn.web.au
{
    public partial class major : System.Web.UI.Page
    {
        XmlDocument doc;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.Attributes["submenu"] = "Undergrad";

            if (Request.QueryString["m"] != null && Request.QueryString["d"] != null)
            {


                string majorName = Request.QueryString["m"].Trim().ToUpper();
                string deptName = Request.QueryString["d"].Trim().ToUpper();

                if (ViewState["MAJOR_" + majorName] == null)
                {
                    doc = new XmlDocument();

                    doc.Load(Server.MapPath("~/App_Data/subjects.xml"));
                    XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
                    xm.AddNamespace("ab", "http://tempuri.org/subject.xsd");

                    XmlNode majorNode = doc.DocumentElement.SelectSingleNode("ab:department[@name='" + deptName + "']/ab:majors/ab:major[@name='" + majorName + "']", xm);
                    if (majorNode != null)
                    {
                        Major m = new Major(majorNode.Attributes["name"].Value,
                            majorNode.SelectSingleNode("ab:name", xm) == null ? string.Empty : majorNode.SelectSingleNode("ab:name", xm).InnerText,
                            majorNode.SelectSingleNode("ab:desc", xm) == null ? string.Empty : majorNode.SelectSingleNode("ab:desc", xm).InnerText,
                            majorNode.SelectSingleNode("ab:career", xm) == null ? string.Empty : majorNode.SelectSingleNode("ab:career", xm).InnerText,
                            majorNode.SelectSingleNode("ab:target", xm) == null ? string.Empty : majorNode.SelectSingleNode("ab:target", xm).InnerText,
                            majorNode.SelectSingleNode("ab:subjects", xm) == null ? string.Empty : majorNode.SelectSingleNode("ab:subjects", xm).InnerText);

                        this.Title = m.MajorChName;

                        ViewState["MAJOR_" + majorName] = m;
                        //Cache["MAJOR_" + majorName] = m;

                    }
                    else
                    {
                        this.Title = "Error!";
                        return;
                    }

                }

                this.DisplayMajor(majorName);

            }
            else
            {
                this.Title = "Error!";
            }
        }

        protected void DisplayMajor(string majorName)
        {
            Major m = (Major)ViewState["MAJOR_" + majorName];
            if (m.Career != string.Empty)
            {
                ltrCareer.Text = m.Career;
            }
            else
            {
                pnCareer.Visible = false;
            }

            if (m.Description != string.Empty)
            {
                ltrMajorDesc.Text = m.Description;
            }
            else
            {
                pnDesc.Visible = false;
            }

            if (m.Target != string.Empty)
            {
                ltrTarget.Text = m.Target;
            }
            else
            {
                pnTarget.Visible = false;
            }

            ltrMajorName.Text = string.Format("{0}<br/>{1}", m.MajorChName.Substring(0, m.MajorChName.IndexOf(' ')), m.MajorChName.Substring(m.MajorChName.IndexOf(' ') + 1));

            string t = Regex.Replace(m.Subjects, @"\r\n", "</td></tr><tr><td>");
            t = Regex.Replace(t, @"\t{2,}", "</td><td>");
            t = Regex.Replace(t, "(First|Second|Third|Fourth) (Semester|Year)", "<em>$1 $2</em>");
            t = Regex.Replace(t, "第(一|二|三|四)(学期|学年|年)", "<em>第$1$2</em>");
            ltrSubjects.Text = "<table class='subject'>" + t + "</table>";
        }
    }
}
